<?php

namespace application\pay\controller;

use application\admin\controller\base as auth;
use ticky\request;
use ticky\response;
use application\pay\model\payment;

//支付操作类
class index extends auth {

    /**
     * 支付宝支付异步回调
     */
    public function pay_callback() {
        if (request::isPost()) {
            $out_trade_no = $_POST['out_trade_no'];
            $order = db('order')->field('id,order_sn,status,userid,username,paytype,money,quantity,`desc`')->where(array('order_sn' => $out_trade_no))->find();
            if (!$order) {
                exit('fail');
            }
            $order_params = array(
                'out_trade_no' => $order['order_sn'],
                'total_amount' => $order['money'],
                'status' => $order['status'],
                'id' => $order['id']
            );
            // 验签和参数校检
            $result = payment::alipay_notify($_POST, $order_params);
            if ($result) {
                $result = payment::alipay_check_status($_POST['trade_status']);
                if ($result) {
                    payment::update_order($order, $_POST['trade_no']);
                }
                echo "success"; //不要修改或删除
            } else {
                echo "fail"; //不要修改或删除
            }

            exit;
        }
    }

    /**
     * 支付宝支付同步跳转通知
     */
    public function pay_return() {
        load_extend('alipay/returnpay.php');
        $result = \returnpay::check($_GET);
        if ($result) {
            showmsg('支付成功，正在返回会员中心！', 'member/index', 2);
        } else {
            showmsg('支付校验失败！', 'stop');
        }
    }

    /**
     * 微信异步通知
     */
    public function wx_notify() {
        load_extend('wxpay/notify.php');
        if (class_exists('notify')) {
            $notify = new \notify();
            $notify->Handle();
        }
    }

}
